package csloader.util
{
	import com.adobe.crypto.MD5;

	public class KeyValuePair
	{
		//内部属性
		public function KeyValuePair()
		{
			_keys = new Vector.<String>;
			_values = new Vector.<String>;
		}
		private var _keys:Vector.<String>;
		public function set Keys(value:Vector.<String>):void
		{
			_keys = value;
		}
		public function get Keys():Vector.<String>
		{
			return _keys;
		}
		private var _values:Vector.<String>;
		public function get Values():Vector.<String>
		{
			return _values;
		}		
		public function set Values(value:Vector.<String>):void
		{
			_values = value;
		}
		public function get length():uint
		{
			return _keys.length;
		}
		//公开方法
		public function sort():void
		{
			var sortedkeys:Vector.<String> = _keys.slice().sort(cp);
			var sortedvalues:Vector.<String> = new Vector.<String>;
			for each(var key:String in sortedkeys)
			{
				sortedvalues.push(_values[_keys.indexOf(key)]);
			}
			_keys = sortedkeys;
			_values = sortedvalues;
		}
		public function clone():KeyValuePair
		{
			var sk:KeyValuePair = new KeyValuePair();
			sk.Keys = _keys.slice();
			sk.Values = _values.slice();
			return sk;
		}
		public function concat(target:KeyValuePair):KeyValuePair
		{
			var sk:KeyValuePair = new KeyValuePair();
			sk.Keys = _keys.concat(target.Keys);
			sk.Values = _values.concat(target.Values);
			return sk;
		}
		private function cp(x:String, y:String):Number
		{
			if(x < y)
				return x == "sign" ? 1 : -1;
			else if(x > y)
				return y == "sign" ? -1 : 1;
			else
				return 0;
		}
		public function addItem(key:String, value:String):void
		{
			var index:int = _keys.indexOf(key);
			if(index >= 0)
			{
				_values[index] = value;
			}
			else
			{
				_keys.push(key);
				_values.push(value);
			}
		}
		public function removeItem(key:String):void
		{
			var index:int = _keys.indexOf(key);
			if(index < 0)
				return;
			
			delete _keys[index];
			delete _values[index];
		}
		public function getItem(key:String):String
		{
			return _values[key];
		}
		public function toString():String
		{
			var str:String = "";
			for(var i:int = 0; i < _keys.length; i++)
			{
				str += _keys[i] + "=" + _values[i] + "&";
			}
			if(str.length>0)
				return str.substr(0,str.length - 1);
			else
				return str;
		}
	}
}